cd "${mystart}/Simulated data\Calibration Minorities/Benchmark Data Minorities/"

* Figure 31
* EMPLOYMENT
qui{
clear
use "Data files/Benchmark.dta"
keep if age<65

tab employment_offer_yn employed_yn
 
replace emp=2 if emp==3 // PT
replace emp=3 if emp==4 // FT, no ins
replace emp=4 if emp==5 // FT, ins
collapse  (count) number_model=ID, by(emp race age)
reshape wide number_model, i(age race) j(emp)
egen total=rowtotal(number_model1 number_model2 number_model3 number_model4)
replace number_model1=number_model1/total*100
replace number_model2=number_model2/total*100
replace number_model3=number_model3/total*100
replace number_model4=number_model4/total*100
sort age race
merge age race using  "${mystart}\CPS\CPS Data Files\Intermediate Data Files\CPS LS Ins Races.dta"
drop _merge
foreach num of numlist 1 2 3 4 {
    rename number`num' number`num'_CPS
}
drop number5 // this is the same as 4
cap drop total
sort age race


sort age
#delimit ;
line number_model2 age if race==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry)
|| line  number_model3 age if race==2, lwidth(medthick) lcolor(edkblue) 
|| line  number_model4 age if race==2, lwidth(medthick) lcolor(green) 
|| line  number2_CPS age if race==2, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line  number3_CPS age if race==2, lwidth(medthick) lcolor(edkblue) lpattern(dash)
|| line  number4_CPS age if race==2, lwidth(medthick) lcolor(green) lpattern(dash)
  ||, 
  ylabel(0.0(10)60)
  xlabel(25(10)65)
  ytitle("Percentage")
xtitle("Age")
  title("Distribution of Employment", color(black))
subtitle("Blacks")
  legend(label(1 "PT, Model") label(2 "FT, no ESHI, Model") label(3 "FT, ESHI, Model")   label(4 "PT, Data") label(5 "FT, no ESHI, Data") label(6 "FT, ESHI, Data") )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\LF1_ESHI_Blacks.eps", replace


sort age
#delimit ;
line number_model2 age if race==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry)
|| line  number_model3 age if race==3, lwidth(medthick) lcolor(edkblue) 
|| line  number_model4 age if race==3, lwidth(medthick) lcolor(green) 
|| line  number2_CPS age if race==3, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line  number3_CPS age if race==3, lwidth(medthick) lcolor(edkblue) lpattern(dash)
|| line  number4_CPS age if race==3, lwidth(medthick) lcolor(green) lpattern(dash)
  ||, 
  ylabel(0.0(10)60)
  xlabel(25(10)65)
  ytitle("Percentage")
xtitle("Age")
  title("Distribution of Employment", color(black))
subtitle("Hispanics")
  legend(label(1 "PT, Model") label(2 "FT, no ESHI, Model") label(3 "FT, ESHI, Model")   label(4 "PT, Data") label(5 "FT, no ESHI, Data") label(6 "FT, ESHI, Data") )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\LF1_ESHI_Hisp.eps", replace


* COMBINE THOSE WITH AND WITHOUT INSURANCE WORKING FT
replace number_model3 = number_model3+ number_model4
drop number_model4
replace number3_CPS = number3_CPS+ number4_CPS
drop number4_CPS

sort age
#delimit ;
line number_model2 age if race==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry)
|| line  number_model3 age if race==2, lwidth(medthick) lcolor(edkblue) 
|| line  number2_CPS age if race==2, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line  number3_CPS age if race==2, lwidth(medthick) lcolor(edkblue) lpattern(dash)
  ||, 
  ylabel(0.0(10)90)
  xlabel(25(10)65)
  ytitle("Percentage")
xtitle("Age")
  title("Blacks", color(black))
  legend(label(1 "PT, Model") label(2 "FT, Model")   label(3 "PT, Data") label(4 "FT, Data")  )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\LF1_Blacks.eps", replace

sort age
#delimit ;
line number_model2 age if race==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry)
|| line  number_model3 age if race==3, lwidth(medthick) lcolor(edkblue) 
|| line  number2_CPS age if race==3, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line  number3_CPS age if race==3, lwidth(medthick) lcolor(edkblue) lpattern(dash)
  ||, 
  ylabel(0.0(10)90)
  xlabel(25(10)65)
  ytitle("Percentage")
xtitle("Age")
  title("Hispanics", color(black))
  legend(label(1 "PT, Model") label(2 "FT, Model")   label(3 "PT, Data") label(4 "FT, Data")  )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\LF1_Hisp.eps", replace
}

* Figure 32
**** GOV TRANSFERS
qui{
clear
use "Data files/Benchmark.dta"
keep if  age<64
		
		sort race age
		collapse (mean)   TR , by(race age) fast
gen data=1 // simulation		
append using   "${mystart}\CPS\CPS Data Files\Intermediate Data Files\CPS TR all Races.dta"		


 #delimit ;
line TR age if race==2 & data==0  , yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  TR age if race==2 & data==1, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.0(.1).6)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction getting Gov Transfers, Blacks", color(black))
  legend(label(1 "CPS Data") label(2 "Model")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr       
graph export "${out_figures}\TR_Profile_races_1.eps", replace



 #delimit ;
line TR age if race==3 & data==0  , yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  TR age if race==3 & data==1, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.0(.1).6)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction getting Gov Transfers, Hispanics", color(black))
  legend(label(1 "CPS Data") label(2 "Model")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr       
graph export "${out_figures}\TR_Profile_races_2.eps", replace


clear
use "Data files/Benchmark.dta"
keep if  age<64
		
		sort education age
		collapse (mean)   TR , by(race married age ) fast
		
		gen data=1 // simulation		
append using   "${mystart}\CPS\CPS Data Files\Intermediate Data Files\CPS TR Married Races.dta"	


 #delimit ;
line TR age if race==2 &  married==0 & data==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  TR age if race==2 & married==0 & data==1, lwidth(medthick) lcolor(cranberry)   
|| line  TR age if race==2 & married==1 & data==0, lwidth(medthick) lcolor(navy)   lpattern(dash)
|| line  TR age if race==2 & married==1 & data==1, lwidth(medthick) lcolor(navy)   
  ||, 
  ylabel(0.0(.1).6)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction getting Gov Transfers, Blacks", color(black))
  legend(label(1 "CPS, Singles") label(2 "Model, Singles")  label(3 "CPS, Married") label(4 "Model, Married") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr       
graph export "${out_figures}\TR_Profile_races_B_mar.eps", replace



 #delimit ;
line TR age if race==3 &  married==0 & data==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  TR age if race==3 & married==0 & data==1, lwidth(medthick) lcolor(cranberry)   
|| line  TR age if race==3 & married==1 & data==0, lwidth(medthick) lcolor(navy)   lpattern(dash)
|| line  TR age if race==3 & married==1 & data==1, lwidth(medthick) lcolor(navy)   
  ||, 
  ylabel(0.0(.1).6)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction getting Gov Transfers, Hispanics", color(black))
  legend(label(1 "CPS, Singles") label(2 "Model, Singles")  label(3 "CPS, Married") label(4 "Model, Married") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr       
graph export "${out_figures}\TR_Profile_races_H_mar.eps", replace
}


* Figure 33
* H and R by age, data and model
qui{
clear
use "Data files/Benchmark.dta"
keep if education==1
replace R_recorded=R_recorded-1
replace R=R-1
collapse (mean)   R R_recorded , by(age race) fast
		replace R_recorded=R_recorded*100
		replace R=R*100
rename race RACE_sum
sort RACE_sum age
merge RACE_sum age using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\R_ages_ALL_races.dta"
sort RACE_sum age
replace R3=R3*100
replace R5=R5*100

drop if age>=65
#delimit ;
line R age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  R_recorded age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  R5 age if RACE_sum==2, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)50)
  xlabel(25(10)65)
  ytitle("Percent")
xtitle("Age")
   title("High R, Blacks", color(black))
  legend(label(1 "R Actual, Model") label(2 "R Recorded, Model") label(3 "R Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\R_Profile_ALL_Black.eps", replace


#delimit ;
line R age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  R_recorded age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  R5 age if RACE_sum==3, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)50)
  xlabel(25(10)65)
  ytitle("Percent")
xtitle("Age")
   title("High R, Hispanics", color(black))
  legend(label(1 "R Actual, Model") label(2 "R Recorded, Model") label(3 "R Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\R_Profile_ALL_Hisp.eps", replace


 
clear
use "Data files/Benchmark.dta"
		gen indic_P=0
		gen indic_A=0
		gen indic_G=0
		replace indic_P=1 if H==1 | H==4
		replace indic_A=1 if H==2
		replace indic_G=1 if H==3
		
replace R_recorded=R_recorded-1
replace R=R-1
		

		
		sort education age_group
		collapse (mean)   dp dp_record du du_record s s_record R R_recorded indic_P indic_A indic_G, by(race age_group) fast
        replace dp=dp*100
		replace dp_record=dp_record*100
		
		        replace du=du*100
		replace du_record=du_record*100
		
		        replace s=s*100
		replace s_record=s_record*100

		replace R_recorded=R_recorded*100
		replace R=R*100
		
		replace indic_P=indic_P*100
		replace indic_A=indic_A*100
		replace indic_G=indic_G*100
		
	
	rename race RACE_sum
		sort RACE_sum age_group	
		
		
merge RACE_sum age_group using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Moments 2 data races.dta"
sort RACE_sum age_group
rename age_group age
*H
#delimit ;
line indic_P age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  indic_A age if RACE_sum==2, lwidth(medthick) lcolor(edkblue) lpattern(solid)
|| line  indic_G age if RACE_sum==2, lwidth(medthick) lcolor(midgreen)
|| line H_1_data age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  H_2_data age if RACE_sum==2, lwidth(medthick) lcolor(edkblue) lpattern(dash)
|| line  H_3_data age if RACE_sum==2, lwidth(medthick) lcolor(midgreen) lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(10)100)
  ytitle("Fraction")
xtitle("Age")
   title("Blacks", color(black))
  legend(label(1 "Poor, Model") label(2 "Fair, Model") label(3 "Good, Model") label(4 "Poor, Data") label(5 "Fair, Data") label(6 "Good, Data") )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Health_Profile1_Black.eps", replace	

#delimit ;
line indic_P age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  indic_A age if RACE_sum==3, lwidth(medthick) lcolor(edkblue) lpattern(solid)
|| line  indic_G age if RACE_sum==3, lwidth(medthick) lcolor(midgreen)
|| line H_1_data age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) lpattern(dash)
|| line  H_2_data age if RACE_sum==3 , lwidth(medthick) lcolor(edkblue) lpattern(dash)
|| line  H_3_data age if RACE_sum==3, lwidth(medthick) lcolor(midgreen) lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(10)100)
  ytitle("Fraction")
xtitle("Age")
   title("Hispanics", color(black))
  legend(label(1 "Poor, Model") label(2 "Fair, Model") label(3 "Good, Model") label(4 "Poor, Data") label(5 "Fair, Data") label(6 "Good, Data") )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Health_Profile1_Hisp.eps", replace	
}


* Figure 34
* health shocks by age, data and model
qui{
clear
use "Data files/Benchmark.dta"
		gen indic_P=0
		gen indic_A=0
		gen indic_G=0
		replace indic_P=1 if H==1 | H==4
		replace indic_A=1 if H==2
		replace indic_G=1 if H==3
		
replace R_recorded=R_recorded-1
replace R=R-1
		
			drop age_group
		rename age age_group
		
		sort education age_group
		collapse (mean)   dp dp_record du du_record s s_record R R_recorded indic_P indic_A indic_G, by(race age_group) fast
        replace dp=dp*100
		replace dp_record=dp_record*100
		
		        replace du=du*100
		replace du_record=du_record*100
		
		        replace s=s*100
		replace s_record=s_record*100

		replace R_recorded=R_recorded*100
		replace R=R*100
		
		replace indic_P=indic_P*100
		replace indic_A=indic_A*100
		replace indic_G=indic_G*100
		
	rename race RACE_sum
		sort RACE_sum age_group		
	
		
merge RACE_sum age_group using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Moments 2 data races.dta"
sort age_group
rename age_group age
	
* dp 
drop if age>=65
replace dp_record =. if age>64
#delimit ;
line dp age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  dp_record age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  dp_data age if RACE_sum==2, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("dp Shocks, Blacks", color(black))
  legend(label(1 "Actual, Model") label(2 "dp Recorded, Model") label(3 "dp Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\dp_Profile1_Blacks_h.eps", replace

replace dp_record =. if age>64
#delimit ;
line dp age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  dp_record age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  dp_data age if RACE_sum==3, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("dp Shocks, Hispanics", color(black))
  legend(label(1 "Actual, Model") label(2 "dp Recorded, Model") label(3 "dp Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\dp_Profile1_Hisp_h.eps", replace


* du

replace du_record =. if age>64
#delimit ;
line du age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  du_record age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  du_data age if RACE_sum==2, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("du Shocks, Blacks", color(black))
  legend(label(1 "Actual, Model") label(2 "du Recorded, Model") label(3 "du Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\du_Profile1_Blacks_h.eps", replace

replace du_record =. if age>64
#delimit ;
line du age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  du_record age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  du_data age if RACE_sum==3, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("du Shocks, Hispanics", color(black))
  legend(label(1 "Actual, Model") label(2 "du Recorded, Model") label(3 "du Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\du_Profile1_Hisp_h.eps", replace


* s
replace s_record =. if age>64
#delimit ;
line s age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  s_record age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  s_data age if RACE_sum==2, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("s Shocks, Blacks", color(black))
  legend(label(1 "Actual, Model") label(2 "s Recorded, Model") label(3 "s Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\s_Profile1_Blacks_h.eps", replace

replace s_record =. if age>64
#delimit ;
line s age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line  s_record age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  s_data age if RACE_sum==3, lwidth(medthick) lcolor(edkblue)  lpattern(dash)
  ||, 
  ylabel(0.0(10)100)
  xlabel(25(5)65)
  ytitle("Fraction")
xtitle("Age")
   title("s Shocks, Hispanics", color(black))
  legend(label(1 "Actual, Model") label(2 "s Recorded, Model") label(3 "s Recorded, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\s_Profile1_Hisp_h.eps", replace	
}

* Figure 35
* H transitions by education and age
qui{
clear
use "Data files/Benchmark.dta"
sort race ID age
gen H_trans=0 if (H==3 ) & H[_n+1]==3  & ID==ID[_n+1]   // stay in good  
replace H_trans=1 if H==3 & (H[_n+1]==2 | H[_n+1]==1)   & ID==ID[_n+1]  // transition to worse health

// from bad to good
gen H_trans1=0 if  (H==2 | H==1) &  (H[_n+1]==2 | H[_n+1]==1)  & ID==ID[_n+1]
replace H_trans1=1 if (H==2 | H==1) &  H[_n+1]==3   & ID==ID[_n+1]

// from poor to better
gen H_trans2=0 if  ( H==1) &  ( H[_n+1]==1)  & ID==ID[_n+1]
replace H_trans2=1 if ( H==1) &  (H[_n+1]==2 |  H[_n+1]==3)  & ID==ID[_n+1]


// from poor to poor
gen H_trans3=0 if  ( H==1) &  ( H[_n+1]!=1)  & ID==ID[_n+1]
replace H_trans3=1 if ( H==1) &  (H[_n+1]==1)  & ID==ID[_n+1]

// from fair to fair
gen H_trans4=0 if  ( H==2) &  ( H[_n+1]!=2)  & ID==ID[_n+1]
replace H_trans4=1 if ( H==2) &  (H[_n+1]==2)  & ID==ID[_n+1]

// from poor to poor
gen H_trans5=0 if  ( H==3) &  ( H[_n+1]!=3)  & ID==ID[_n+1]
replace H_trans5=1 if ( H==3) &  (H[_n+1]==3)  & ID==ID[_n+1]


// from fair/good  to fair/good
gen H_trans6=0 if  ( H==3 | H==2) &  ( H[_n+1]==1)  & ID==ID[_n+1]
replace H_trans6=1 if  ( H==3 | H==2) &  ( H[_n+1]!=1)  & ID==ID[_n+1]


keep if age>=25 & age<75

collapse (mean) GB_model=H_trans BG_model=H_trans1  PG_model=H_trans2 PP_model=H_trans3  FF_model=H_trans4  GG_model=H_trans5 FGP_model=H_trans6, by(race  age_group) 
rename age_group age
rename race RACE_sum
sort  RACE_sum age


merge  RACE_sum age using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Trans Moments data races.dta"

sort age
#delimit ;
line GB_model age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line GB_model age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line GB age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line GB  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.1).6)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Good to Fair/Poor", color(black))
  legend(label(1 "Blacks, Model") label(2 "Hispanics, Model") label(3 "Blacks, Data") label(4 "Hispanics, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_GB_Races.eps", replace

sort age
#delimit ;
line BG_model age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line BG_model age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line BG age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line BG  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.1).7)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Fair/Poor to Good", color(black))
  legend(label(1 "Blacks, Model") label(2 "Hispanics, Model") label(3 "Blacks, Data") label(4 "Hispanics, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_BG_Races.eps", replace


sort age
#delimit ;
line PG_model age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line PG_model age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line PG age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line PG  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.1)1)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Poor to Fair/Good", color(black))
  legend(label(1 "Blacks, Model") label(2 "Hispanics, Model") label(3 "Blacks, Data") label(4 "Hispanics, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_PG_Races.eps", replace


gen FGPP_model = 1- FGP_model
gen FGPP = 1- FGP



sort age
#delimit ;
line FGPP_model age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line FGPP_model age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line FGPP age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line FGPP  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.05).2)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Fair/Good to Poor", color(black))
  legend(label(1 "Blacks, Model") label(2 "Hispanics, Model") label(3 "Blacks, Data") label(4 "Hispanics, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_FGP_Races.eps", replace
}


* Figure 36
* H transitions Good to Good 
qui{
clear
use "Data files/Benchmark.dta"
sort race ID age
gen H_trans=1 if (H==3 ) & H[_n+1]==3  & ID==ID[_n+1]   // stay in good  
replace H_trans=0 if H==3 & (H[_n+1]==2 | H[_n+1]==1)   & ID==ID[_n+1]  // transition to worse health


keep if age>=25 & age<75
gen d_shock=0 if du_record==0 & dp_record==0 & age<65
replace d_shock=1 if (du_record==1 | dp_record==1) & age<65
replace d_shock=0 if du==0 & dp==0 & age>64
replace d_shock=1 if (du==1 | dp==1) & age>64

collapse (mean) GG_model=H_trans, by(race  age_group d_shock) 
rename age_group age
rename race RACE_sum
sort RACE_sum age d_shock


merge RACE_sum age d_shock using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Trans Moments data shocks races.dta"

drop _merge

sort age
#delimit ;
line GG_model  age if d_shock==0 & RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line GG_model  age if d_shock==1 & RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line GG  age if d_shock==0 & RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line GG age if d_shock==1 & RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.2)1)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Good to Good, Blacks", color(black))
  legend(label(1 "No Recorded d{superscript:p} or d{superscript:u}, Model") label(2 "Recorded d{superscript:p} or d{superscript:u}, Model")   label(3 "No Recorded d{superscript:p} or d{superscript:u}, Data") label(4 "Recorded d{superscript:p} or d{superscript:u}, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_GG_shock1_Blacks.eps", replace

#delimit ;
line GG_model  age if d_shock==0 & RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line GG_model  age if d_shock==1 & RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line GG  age if d_shock==0 & RACE_sum==3, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line GG age if d_shock==1 & RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.2)1)
  xlabel(25(5)74)
  ytitle("Fraction")
xtitle("Age")
   title("H Transitions from Good to Good, Hispanics", color(black))
  legend(label(1 "No Recorded d{superscript:p} or d{superscript:u}, Model") label(2 "Recorded d{superscript:p} or d{superscript:u}, Model")   label(3 "No Recorded d{superscript:p} or d{superscript:u}, Data") label(4 "Recorded d{superscript:p} or d{superscript:u}, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Trans_Profile_GG_shock1_Hisp.eps", replace
}



* Figure 37 
* H by insurance and employment
qui{
* by ESHI
clear
use "Data files/Benchmark.dta"
keep if age<65 
gen H_1=0 
replace H_1=1 if H==1
gen H_2=0 
replace H_2=1 if H==2
gen H_3=0 
replace H_3=1 if H==3
collapse H_1 H_2 H_3, by(age race ESHI)
sort age
rename race RACE_sum
reshape wide H*, i(age RACE_sum) j(ESHI)
sort RACE_sum age
merge RACE_sum age using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Moments 3 data races.dta"
drop if age>64

foreach var of varlist H_10 - H_3_data1{
	replace `var' = 1-`var'
}

sort age
#delimit ;
line H_30 age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry)
|| line H_31 age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line H_3_data0 age if RACE_sum==2  , lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line H_3_data1  age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.10)1)
  xlabel(25(5)64)
  title("Blacks", color(black))
  ytitle("Fraction")
xtitle("Age")
  legend(label(1 "No ESHI, Model") label(2 "ESHI, Model") label(3 "No ESHI, Data")  label(4 "ESHI, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Profile4_HS_Black.eps", replace


#delimit ;
line H_30 age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line H_31 age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line H_3_data0 age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line H_3_data1  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.10)1)
  xlabel(25(5)64)
  title("Hispanics", color(black))
  ytitle("Fraction")
xtitle("Age")
  legend(label(1 "No ESHI, Model") label(2 "ESHI, Model") label(3 "No ESHI, Data")  label(4 "ESHI, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Profile4_HS_Hisp.eps", replace


* BY EMPLOYMENT
clear
use "Data files/Benchmark.dta"
keep if age<65 
gen H_1=0 
replace H_1=1 if H==1
gen H_2=0 
replace H_2=1 if H==2
gen H_3=0 
replace H_3=1 if H==3
collapse H_1 H_2 H_3, by(age race employed_yn)
sort age
reshape wide H*, i(race age) j(employed_yn)
sort race age
rename race RACE_sum
merge RACE_sum age using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health Moments 6 data races.dta"
drop if age>64

foreach var of varlist H_10 - H_3_data1{
	replace `var'=1-`var'
}


sort age
#delimit ;
line H_30 age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line H_31 age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line H_3_data0 age if RACE_sum==2, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line H_3_data1  age if RACE_sum==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.10)1)
  xlabel(25(5)64)
     title("Blacks", color(black))
  ytitle("Fraction")
xtitle("Age")
  legend(label(1 "Not Employed, Model") label(2 "Employed, Model") label(3 "Not Employed, Data")  label(4 "Employed, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Profile16_HS_Blacks.eps", replace

sort age
#delimit ;
line H_30 age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line H_31 age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
|| line H_3_data0 age if RACE_sum==3, lwidth(medthick) lcolor(cranberry)  lwidth(medthick) lpattern(dash)
|| line H_3_data1  age if RACE_sum==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
  ||, 
  ylabel(0.0(.10)1)
  xlabel(25(5)64)
  title("Hispanics", color(black))
  ytitle("Fraction")
xtitle("Age")
  legend(label(1 "Not Employed, Model") label(2 "Employed, Model") label(3 "Not Employed, Data")  label(4 "Employed, Data") )
  legend(col(2) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Health_Profile16_HS_Hisp.eps", replace
}


* Figure 38
* MEDICAL CHARGES BY AGE AND INSURANCE 
qui{
clear
use "Data files/Benchmark.dta"
keep if age<65
collapse (mean)	MC, by(age_group INSURANCE race)
replace MC=MC/1000
reshape wide MC, i(age_group race) j(INSURANCE)
sort age_group race
merge age_group race using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Charges Moments 2 data Races.dta"

 #delimit ;
line MC2 age_group if race==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(green) 
|| line  MC1 age_group if race==2, lwidth(medthick)  lcolor(cranberry)
|| line  MC3 age_group if race==2, lwidth(medthick) lcolor(edkblue) 
|| line  Charges_plus_RX2 age_group if race==2, lwidth(medthick) lcolor(green)    lpattern(dash)
|| line  Charges_plus_RX1 age_group if race==2, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  Charges_plus_RX3 age_group if race==2, lwidth(medthick) lcolor(edkblue)   lpattern(dash)
  ||, 
  ylabel(0.0(4)40)
  xlabel(25(5)65)
  ytitle("Charges (thousands)")
xtitle("Age")
   title("Average Medical Charges, MEPS and Model, Blacks", color(black))
  legend(label(1 "Public, Model") label(2 "ESHI, Model") label(3 "Uninsured, Model") label(4 "Public, Data") label(5 "ESHI, Data") label(6 "Uninsured, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Charges_profile1_ALL_Blacks.eps", replace


 #delimit ;
line MC2 age_group if race==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(green) 
|| line  MC1 age_group if race==3, lwidth(medthick)  lcolor(cranberry)
|| line  MC3 age_group if race==3, lwidth(medthick) lcolor(edkblue) 
|| line  Charges_plus_RX2 age_group if race==3, lwidth(medthick) lcolor(green)    lpattern(dash)
|| line  Charges_plus_RX1 age_group if race==3, lwidth(medthick) lcolor(cranberry)   lpattern(dash)
|| line  Charges_plus_RX3 age_group if race==3, lwidth(medthick) lcolor(edkblue)   lpattern(dash)
  ||, 
  ylabel(0.0(4)40)
  xlabel(25(5)65)
  ytitle("Charges (thousands)")
xtitle("Age")
   title("Average Medical Charges, MEPS and Model, Hispanics", color(black))
  legend(label(1 "Public, Model") label(2 "ESHI, Model") label(3 "Uninsured, Model") label(4 "Public, Data") label(5 "ESHI, Data") label(6 "Uninsured, Data")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\Charges_profile1_ALL_Hisp.eps", replace
}


 

